<template>
  <el-config-provider :locale="zhCn">
    <div class="app-container">
      <el-container>
        <!-- Header 区域 -->
        <el-header height="60px">
          <div class="header-content">
            <div class="logo" @click="$router.push('/')">
              <el-icon :size="24">
                <school />
              </el-icon>
              <h1>学生奖惩管理系统</h1>
            </div>

            <!-- 用户信息 -->
            <div class="user-control">
              <div v-if="isAuthenticated" class="user-info">
                <el-dropdown>
                  <span class="user-name">
                    <el-icon>
                      <user />
                    </el-icon>
                    {{ userInfo.name || '用户' }}
                  </span>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
              </div>
              <el-button v-else type="primary" @click="$router.push('/login')" class="login-btn">
                立即登录
              </el-button>
            </div>
          </div>
        </el-header>

        <el-container>
          <!-- 侧边导航菜单 -->
          <el-aside width="200px" v-if="isAuthenticated">
            <el-menu
              :router="true"
              :default-active="activeMenu"
            >
              <!-- 管理员菜单 -->
              <template v-if="isAdmin">
                <el-menu-item index="/classes">
                  <el-icon>
                    <school />
                  </el-icon>
                  <span>班级管理</span>
                </el-menu-item>
                <el-menu-item index="/students">
                  <el-icon>
                    <user />
                  </el-icon>
                  <span>学生管理</span>
                </el-menu-item>
                <el-menu-item index="/punishments">
                  <el-icon>
                    <warning />
                  </el-icon>
                  <span>惩罚管理</span>
                </el-menu-item>
                <el-menu-item index="/rewards">
                  <el-icon>
                    <trophy />
                  </el-icon>
                  <span>奖励管理</span>
                </el-menu-item>
              </template>

              <!-- 学生菜单 -->
              <template v-else-if="isStudent">
                <el-menu-item index="/my-rewards">
                  <el-icon>
                    <trophy />
                  </el-icon>
                  <span>我的奖励</span>
                </el-menu-item>
                <el-menu-item index="/my-punishments">
                  <el-icon>
                    <warning />
                  </el-icon>
                  <span>我的处罚</span>
                </el-menu-item>
                <el-menu-item index="/my-profile">
                  <el-icon>
                    <user />
                  </el-icon>
                  <span>我的信息</span>
                </el-menu-item>
              </template>

              <!-- OCR 识别功能 -->
              <el-menu-item index="/ocr" class="ocr-menu-item">
                <el-icon>
                  <document />
                </el-icon>
                <span>OCR识别</span>
              </el-menu-item>
            </el-menu>
          </el-aside>

          <!-- Main 内容区域 -->
          <el-main>
            <router-view />
          </el-main>
        </el-container>

        <!-- Footer 区域 -->
        <el-footer height="50px">
          <div class="footer-content">
            © {{ new Date().getFullYear() }} 江西师范大学学生奖惩管理系统 - 版权所有
          </div>
        </el-footer>
      </el-container>
    </div>
  </el-config-provider>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useStore } from 'vuex'
import {
  School, User, House, Warning, Trophy, Document
} from '@element-plus/icons-vue'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'

const route = useRoute()
const router = useRouter()
const store = useStore()

// 计算属性
const activeMenu = computed(() => route.path)
const isAuthenticated = computed(() => store.getters.isAuthenticated)
const isAdmin = computed(() => store.getters.isAdmin)
const isStudent = computed(() => store.getters.isStudent)
const userInfo = computed(() => store.state.userInfo || {})

// 登出处理
const handleLogout = () => {
  store.dispatch('logout')
  router.push('/login')
}
</script>

<style scoped>
.app-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.el-header {
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 10;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 0 20px;
}

.logo {
  display: flex;
  align-items: center;
  cursor: pointer;
  gap: 10px;
}

.logo h1 {
  margin: 0;
  font-size: 20px;
  color: #409EFF;
}

.user-control {
  display: flex;
  align-items: center;
  gap: 20px;
}

.user-info .user-name {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #606266;
  cursor: pointer;
  transition: color 0.3s;
}

.user-info .user-name:hover {
  color: #409EFF;
}

.login-btn {
  margin-left: auto;
}

.el-main {
  background-color: #f5f7fa;
  padding: 20px;
  flex: 1;
}

.el-footer {
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  border-top: 1px solid #eee;
}

.footer-content {
  font-size: 14px;
  color: #606266;
}

.ocr-menu-item {
  margin-top: 20px;
  border-top: 1px solid #eee;
  padding-top: 20px;
}

.ocr-menu-item .el-icon {
  color: #67C23A;
}

.ocr-menu-item span {
  color: #67C23A;
  font-weight: bold;
}
</style>